package com.flux.utils

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.support.annotation.DrawableRes
import android.support.v4.app.Fragment
import android.widget.ImageView

import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition

/**
 * 图片加载工具
 * Created on 2017/11/3.
 *
 * @author Yang
 */
object GlideUtil {

    private var mErrorImageRes = -1

    fun setErrorImageRes(mErrorImageRes: Int) {
        GlideUtil.mErrorImageRes = mErrorImageRes
    }

    /**
     * 无默认图加载
     */
    fun <T> load(context: Context, src: T, imageView: ImageView) {
        if (preLoad(src, imageView, mErrorImageRes)) return
        Glide.with(context).load(src).apply(RequestOptions().error(mErrorImageRes).placeholder(mErrorImageRes)).into(imageView)
    }

    /**
     * 默认图加载
     */
    fun <T> load(src: T, imageView: ImageView, @DrawableRes holderRes: Int = mErrorImageRes, fragment: Fragment? = null, activity: Activity? = null) {
        if (preLoad(src, imageView, mErrorImageRes)) return
        if (fragment != null) Glide.with(fragment).load(src).apply(RequestOptions().error(holderRes).placeholder(holderRes)).into(imageView)
        else if (activity != null) Glide.with(activity).load(src).apply(RequestOptions().error(holderRes).placeholder(holderRes)).into(imageView)
    }

    /**
     * 加载圆形
     */
    fun <T> loadCircle(src: T, imageView: ImageView, @DrawableRes holderRes: Int = mErrorImageRes, fragment: Fragment? = null, activity: Activity? = null) {
        if (preLoad(src, imageView, mErrorImageRes)) return
        if (fragment != null) Glide.with(fragment).load(src).apply(RequestOptions().error(holderRes).placeholder(holderRes).circleCrop()).into(imageView)
        else if (activity != null) Glide.with(activity).load(src).apply(RequestOptions().error(holderRes).placeholder(holderRes).circleCrop()).into(imageView)
    }

    /**
     * 加载圆角
     */
    fun <T> loadWithRoundCorners(src: T, imageView: ImageView, @DrawableRes holderRes: Int = mErrorImageRes, radius: Int, fragment: Fragment? = null, activity: Activity? = null) {
        if (preLoad(src, imageView, mErrorImageRes)) return
        if (fragment != null) Glide.with(fragment).load(src)
                .apply(RequestOptions().error(holderRes).placeholder(holderRes).transform(MultiTransformation(CenterCrop(), RoundedCorners(radius)))).into(imageView)
        else if (activity != null) Glide.with(activity).load(src)
                .apply(RequestOptions().error(holderRes).placeholder(holderRes).transform(MultiTransformation(CenterCrop(), RoundedCorners(radius)))).into(imageView)
    }

    /**
     * 加载前工作
     */
    private fun <T> preLoad(src: T?, imageView: ImageView, @DrawableRes holderRes: Int): Boolean {
        imageView.setImageResource(holderRes)
        return src == null
    }

    /**
     * 带回调的图片加载（仅加载网络图片）
     *
     * @param context   上下文
     * @param src       图片地址
     * @param imageView 加载图片的控件
     * @param callBack  回调
     */
    fun loadWithCallback(context: Context, src: String, imageView: ImageView, callBack: GlideCallBack) {
        Glide.with(context).load(src).into(object : SimpleTarget<Drawable>() {
            override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                imageView.setImageDrawable(resource)
                callBack.onGetBitmap(null)
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {

            }
        })
    }

    /**
     * 获取网络图片的Bitmap
     *
     * @param context  上下文
     * @param src      图片地址
     * @param callBack 回调
     */
    fun getBitmapFromUrl(context: Context, src: String, callBack: GlideCallBack) {
        Glide.with(context).asBitmap().load(src).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                LogUtil.i("bitmap loaded success")
                callBack.onGetBitmap(resource)
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                LogUtil.i("bitmap loaded fail")
                callBack.onGetBitmap(null)
            }
        })
    }

    /**
     * 回调
     */
    interface GlideCallBack {
        /**
         * 回调
         *
         * @param bitmap Bitmap
         */
        fun onGetBitmap(bitmap: Bitmap?)
    }

    /**
     * 构建GlideEngine使用
     */
    fun loadThumbnail(context: Context, resize: Int, placeholder: Drawable, imageView: ImageView, uri: Uri) {
        Glide.with(context).asBitmap().load(uri).apply(RequestOptions().placeholder(placeholder).override(resize, resize).centerCrop()).into(imageView)
    }

    /**
     * 构建GlideEngine使用
     */
    fun loadAnimatedGifThumbnail(context: Context, resize: Int, placeholder: Drawable, imageView: ImageView, uri: Uri) {
        Glide.with(context).asBitmap().load(uri).apply(RequestOptions().placeholder(placeholder).override(resize, resize).centerCrop()).into(imageView)
    }

    /**
     * 构建GlideEngine使用
     */
    fun loadImage(context: Context, resizeX: Int, resizeY: Int, imageView: ImageView, uri: Uri) {
        Glide.with(context).asBitmap().load(uri).apply(RequestOptions().override(resizeX, resizeY).priority(Priority.HIGH)).into(imageView)
    }

    /**
     * 构建GlideEngine使用
     */
    fun loadAnimatedGifImage(context: Context, resizeX: Int, resizeY: Int, imageView: ImageView, uri: Uri) {
        Glide.with(context).asGif().load(uri).apply(RequestOptions().override(resizeX, resizeY).priority(Priority.HIGH)).into(imageView)
    }
}